package com.jiancong.controller;

import com.github.pagehelper.Page;
import com.jiancong.domain.ResponseResult;
import com.jiancong.domain.dto.UserDetailDto;
import com.jiancong.domain.dto.UserQueryCriteria;
import com.jiancong.domain.dto.UserUpdateDto;
import com.jiancong.domain.entity.User;
import com.jiancong.domain.vo.UserVo;
import com.jiancong.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/system")
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/user/list")
    public ResponseResult getUserList(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                      @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                      @RequestParam(required = false) String userName,
                                      @RequestParam(required = false) String phonenumber,
                                      @RequestParam(required = false) String status) {
        UserQueryCriteria criteria = new UserQueryCriteria();
        criteria.setPageNum(pageNum);
        criteria.setPageSize(pageSize);
        criteria.setUserName(userName);
        criteria.setPhonenumber(phonenumber);
        criteria.setStatus(status);
        return userService.getUserPageList(criteria);
    }

    @PostMapping("/user")
    public ResponseResult createUser(@RequestBody User user) {
            boolean created = userService.createUser(user);
            return ResponseResult.okResult(created);
    }
    @DeleteMapping("/user/{id}")
    public ResponseResult deleteUser(@PathVariable Long id) {
        boolean deleted = userService.deleteUser(id);
        return ResponseResult.okResult(deleted);
    }

    @GetMapping("/user/{id}")
    public ResponseResult getUserDetail(@PathVariable Long id) {
        UserDetailDto response = userService.getUserDetail(id);
            return ResponseResult.okResult(response);
    }
    @PutMapping("/user")
    public ResponseResult updateUser(@RequestBody UserUpdateDto userUpdateDto) {
        boolean updated = userService.updateUser(userUpdateDto);
        return ResponseResult.okResult(updated);
    }
}